package handler;

import entity.Pet;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CustomBeanPetListHandler implements ResultSetHandler<List<Pet>> {


    @Override
    public List<Pet> handle(ResultSet resultSet) throws SQLException {
        List<Pet> petList = new ArrayList<>();
        while (resultSet.next()) {
            Pet pet = new Pet();
            pet.setId(resultSet.getInt("id"));
            pet.setPetName(resultSet.getString("petName"));
            pet.setPetType(resultSet.getString("petType"));
            pet.setSex(resultSet.getString("sex"));
            java.util.Date sqlDate = resultSet.getDate("birthday");
            if (sqlDate != null) {
               pet.setBirthday(Date.valueOf(((Date) sqlDate).toLocalDate()));
            }else {
                pet.setBirthday(null);
            }
            pet.setBirthday(resultSet.getDate("birthday"));
            pet.setPic(resultSet.getString("pic"));
            pet.setState(resultSet.getInt("state"));
            pet.setRemark(resultSet.getString("remark"));
            petList.add(pet);
        }
        return petList;
    }

}
